package com.lxb.myforum.service.spec

import java.util.ArrayList

import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root

import org.apache.commons.lang3.StringUtils
import org.springframework.data.jpa.domain.Specification

import com.lxb.myforum.pojo.Comment

class CommentSpec(private val comment: Comment) : Specification<Comment> {

    override fun toPredicate(root: Root<Comment>, query: CriteriaQuery<*>, cb: CriteriaBuilder): Predicate? {
        val predicates = ArrayList<Predicate>()

        if (comment.article != null) {
            predicates.add(cb.equal(root.get<Any>("article"), comment.article))
        }

        if (StringUtils.isNotBlank(comment.content)) {
            predicates.add(cb.like(root.get("content"), "%" + comment.content + "%"))
        }
        query.where(*predicates.toTypedArray())
        return null
    }

}
